﻿using System.Linq;
using System.Data.Linq;
using System.Management;

using LinqToWmi.Core.WMI;
using System.Collections.Generic;

namespace System.Net.DnsToolkit.Resolver {
    public class WmiDnsServerProvider : IDnsServerProvider {
        public IPAddress[] Provide() {

            using (WmiContext context = new WmiContext(@"\\.")) {

                context.ManagementScope.Options.Impersonation = ImpersonationLevel.Impersonate;

                var dns = from nic in context.Source<Win32_NetworkAdapterConfiguration>()
                          where nic.IPEnabled
                          select nic;

                var ips = from s in dns.SelectMany(n => n.DNSServerSearchOrder)
                          select IPAddress.Parse(s);

                return ips.ToArray();
            }
        }
    }
}

